<?php

//include("utils.php");
ob_start();
session_start();
include("lang_buildCustomNetwork.php");
include("UserIncludes/DbConfig.php");
// if(!isset($_SESSION[siteuserId]))
// 	echo '<script>location.href="index.php"</script>';
//main_content("mainBuySpace();");
mainBuySpace();
function mainBuySpace()
{
?>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/listing.css" type="text/css" />
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link href="css/global.css" rel="stylesheet" type="text/css">
<?php
if($_SESSION['langId']!="")
{
	$langId=$_SESSION['langId'];
}
else
{
	$langId="1";
}

if(isset($_GET['form_id']))
{
$_SESSION['form_id'] = $_GET['form_id'];

$query_check = "select * from temp_create_network where identity = '".$_SESSION['form_id']."' ";
$res_check = mysql_query($query_check);
$row_check = mysql_fetch_array($res_check);

$category = $row_check['category'];
$category_array = explode(",",$category);
$cat_len = count($category_array);

$country = $row_check['country'];
$country_array = explode(",",$country);
$coun_len = count($country_array);
}

$execCategory = mysql_query("select * from adcategories where status='Y' and langId='".$langId."'");
$execCountry = mysql_query("select * from country where status='Y'");
$sqlNetworkAds = "select * from ".NETWORKMASTER.",".NETWORKDETAILS." where ".NETWORKMASTER.".status='Y' AND ".NETWORKMASTER.".networkId=".NETWORKDETAILS.".networkId";
$execNetworkAds = mysql_query($sqlNetworkAds);

if(isset($_POST[btnSaveNetwork]))
{
 	$val_category=$_POST['selCategory'];
 	$val_geography = $_POST['selGeography'];
	$all_geo_value = '';

	foreach($val_geography as $value_geography)
	{
		
		$all_geo_value = $all_geo_value.$value_geography.",";
	}

	$len_geo = strlen($all_geo_value);
	$final_geo_string = substr($all_geo_value,0,($len_geo-1));

	$all_category_value = '';
	foreach($val_category as $value_category)
	{
		$all_category_value = $all_category_value.$value_category.",";

	}

	$len_category = strlen($all_category_value);
	$final_category_string = substr($all_category_value,0,($len_category-1));
	$main_adgroup = "main_adgroup";

	//	$query_temp = " INSERT into temp_create_network(`identity`, `ad_type`, `bid`, `per`, `network`, `banner_size`, `category`, `country`, `userid`) values('".$main_adgroup."','".$_POST['adtype']."','".$_POST['max_bid']."','".$_POST['ad_unit']."','".$_POST['network']."','".$_POST['banner_size']."','".$final_category_string."','".$final_geo_string."',".$_SESSION['siteuserId'].") ";
	if(mysql_num_rows($res_check)>0)
	{
	$query_temp = "UPDATE temp_create_network SET `identity`= '".$_SESSION['form_id']."',`ad_type` = '".$_POST['adtype']."',`bid`='".$_POST['max_bid']."',`per`='".$_POST['ad_unit']."',`network`='".$_POST['network']."',`banner_size`='".$_POST['banner_size']."',`category`='".$final_category_string."',`country`='".$final_geo_string."',`userid` = ".$_SESSION['siteuserId']." where `identity`= '".$_SESSION['form_id']."' ";
	}
	
	else
	{
		if($_REQUEST['ron']=='ron')
		{
			$query_temp = " INSERT into temp_create_network(`identity`, `ad_type`, `bid`, `per`, `network`, `banner_size`, `category`, `country`, `userid`, `RON`) values('".$_SESSION['form_id']."','".$_POST['adtype']."','".$_POST['max_bid']."','".$_POST['ad_unit']."','".$_POST['network']."','".$_POST['banner_size']."','".$final_category_string."','".$final_geo_string."',".$_SESSION['siteuserId'].",'Y') ";
		}
		else
		{
			$query_temp = " INSERT into temp_create_network(`identity`, `ad_type`, `bid`, `per`, `network`, `banner_size`, `category`, `country`, `userid`) values('".$_SESSION['form_id']."','".$_POST['adtype']."','".$_POST['max_bid']."','".$_POST['ad_unit']."','".$_POST['network']."','".$_POST['banner_size']."','".$final_category_string."','".$final_geo_string."',".$_SESSION['siteuserId'].")";
		}
	}
	//echo $query_temp;
	mysql_query($query_temp);
?>
<!--################### code updated on 26/06/09 starts ########################-->

<?php

?>
<!--################### code updated on 26/06/09 ends ########################-->
<?php
	echo "<script>parent.parent.GB_hide();</script>";

}

?>

<script type="text/javascript">
function showHide(toShow,toHide)
{
	document.getElementById(toShow).style.display='';
	document.getElementById(toHide).style.display='none';
	if(toShow=='tdCategory')
	{
		document.getElementById('passValue1').value = 'categoryList';
		document.getElementById('passValue2').value =  'selCategory[]';
	}	
	else
	{
		document.getElementById('passValue1').value = 'geographyList';
		document.getElementById('passValue2').value =  'selGeography[]';
	}
}
function moveSelectedOptions(f, t, l, s, d)
{
 var from = document.getElementById(f);
 var to = document.getElementById(t);
 var limit = 0;
 var sort = "text";
 if (s.length > 0)
 sort = s;
 var direction = d;
 if (l.length > 0)
 limit = parseInt(l);
 var msg = "";
if (limit > 0 && to.options.length >= limit)
 {
 msg = "You have already selected the maximum number of options allowed.";
 }
 else
 {
 // Move them over
 var cnt = to.options.length;
 var selectAll = false;
 if (to.options.length == 1 && to.options[0].text.indexOf(" All ") > -1 &&
 direction == ">")
 selectAll = true;
 for (var i = 0; i < from.options.length; i++)
 {
	
	var o = from.options[i];
	if( (o.selected && cnt < limit) || (o.selected && limit == 0))
	{
		if (selectAll)
		{
			from.options[from.options.length] =
			new Option( to.options[0].text, to.options[0].value, false, false);
			to.options[0] = null;
			selectAll = false;
			cnt--;
		}
		if (o.text.indexOf(" All ") > -1 && direction == ">")
		{
		moveAllOptions(t, f);		// Block By ALOK
		//alert('a');
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			//to.options[to.options.length].selected = true;
			from.options[i] = null;
			from.selectedIndex = -1;
			cnt = limit;
			
		}
		else
		{
			//alert(to.name);
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			//to.options[i].selected = true;
			cnt++;
		}
	}
	else if (o.selected && limit > 0)
	{
		o.selected = false;
		if (msg.length == 0)
		msg = "You have exceeded the number of options allowed.  The following options from your selected list have been omitted:\n\n";
		msg += o.text + "\n";
	}
 }

 // Delete them from original
 for (var i = (from.options.length-1); i >= 0; i--)
 {

 var o = from.options[i];
 if (o.selected)
 {
 from.options[i] = null;
 }
 }
 sortSelect(from, sort);
 sortSelect1(to, sort);
 //from.selectedIndex = -1;
// to.selectedIndex = -1;
if(direction =='>'){
	
	from.selectedIndex = -1;
}
if(direction =='<'){
	
	to.selectedIndex = -1;
}

 }
  if (msg.length > 0)
 alert(msg);

//alert(document.getElementById(t).value);
// to.selectedIndex=" All ";
// to.selectedIndex=0;
//document.getElementById('selCategory').selected;
}
function sortSelect1(obj, s)
{
 var o = new Array();
 if (obj.options==null)
 return;
 for (var i = 0; i < obj.options.length; i++)
 {
 o[o.length] =
 new Option(
 obj.options[i].text,
 obj.options[i].value,
 obj.options[i].defaultSelected,
 obj.options[i].selected);

 }
 if (o.length==0)
 return;
 o = o.sort(
 function(a,b)
 {
 if (a.text+"" == "United States") { return -1; }
 else if (b.text+"" == "United States") { return 1; }
 else if (a.value == 9999) { return -1; }
 else if (b.value == 9999) { return 1; }
  if (s == "text")
 {
 if ((a.text+"") < (b.text+"")) { return -1; }
 if ((a.text+"") > (b.text+"")) { return 1; }
 }
 else if (s == "value")
 {
 if ((a.value) < (b.value)) { return -1; }
 if ((a.value) > (b.value)) { return 1; }
 }
 return 0;
 });
 for (var i = 0; i < o.length; i++)
 {

 obj.options[i] =
 new Option(o[i].text,
 o[i].value,
 o[i].defaultSelected,
 o[i].selected);
	
	if(!obj.options[i].selected) 
	{
		obj.options[i].selected=true;
		
	}
 }
}
function sortSelect(obj, s)
{
 var o = new Array();
 if (obj.options==null)
 return;
 for (var i = 0; i < obj.options.length; i++)
 {
 o[o.length] =
 new Option(
 obj.options[i].text,
 obj.options[i].value,
 obj.options[i].defaultSelected,
 obj.options[i].selected);
 }
 if (o.length==0)
 return;
 o = o.sort(
 function(a,b)
 {
 if (a.text+"" == "United States") { return -1; }
 else if (b.text+"" == "United States") { return 1; }
 else if (a.value == 9999) { return -1; }
 else if (b.value == 9999) { return 1; }
  if (s == "text")
 {
 if ((a.text+"") < (b.text+"")) { return -1; }
 if ((a.text+"") > (b.text+"")) { return 1; }
 }
 else if (s == "value")
 {
 if ((a.value) < (b.value)) { return -1; }
 if ((a.value) > (b.value)) { return 1; }
 }
 return 0;
 });
 for (var i = 0; i < o.length; i++)
 {
	
 obj.options[i] =
 new Option(o[i].text,
 o[i].value,
 o[i].defaultSelected,
 o[i].selected);
if(!obj.options[i].selected) 
	{
		obj.options[i].selected=true;
		
	}
 }
}


function disable_unable()
{
	if(document.getElementById('ron').checked == true) {
	
	document.getElementById('categoryList').disabled = true;


	document.getElementById('geographyList').disabled = true;

							   }

	else						   {
	
	document.getElementById('categoryList').disabled = false;

	document.getElementById('geographyList').disabled = false;

							   }
}


function confirm_msg(formID)
{ 

	if (document.getElementById(formID)) {
		var form = document.getElementById(formID);
	} else {
		return;
	}

	// CONFIRM REQUIRES ONE ARGUMENT
	var message = "Please confirm it is OK to proceed...";

	// CONFIRM IS BOOLEAN. THAT MEANS THAT
	// IT RETURNS TRUE IF 'OK' IS CLICKED
	// OTHERWISE IT RETURN FALSE
	var return_value = confirm(message);
	// DISPLAY THE RETURNED VALUE IN THE
	// CONFIRM POPUP DEMO TEXT INPUT BOX
	//form.return_value.value = return_value;

	// TEST TO SEE IF TRUE|FALSE RETURNED
	if ( return_value === true ) {
		// YOUR 'OK' SCRIPT GOES HERE

 		//window.location="http://esolz.org/admobweb/create_campaign.php";
		setTimeout("redirect()",500);

		
		
	} else {
		// YOUR 'CANCEL' SCRIPT GOES HERE
		return false;
	}

}

function redirect()
{
window.location = 'create_campaign.php';
}

function type_setting()
{

	if(document.getElementById('adtype').value == 'text')
	{
	document.getElementById('adUnit').value = 'CPC';
	document.getElementById('banner_size').style.display = 'none';

	//document.all.selectID.selectedIndex = value // IE only
	}

	if(document.getElementById('adtype').value == 'thumbnail')
	{
	document.getElementById('adUnit').value = 'CPC';
	document.getElementById('banner_size').style.display = 'none';

	}

	if(document.getElementById('adtype').value == 'banner')
	{
	document.getElementById('adUnit').value = 'CPM';
	document.getElementById('banner_size').style.display = 'block';
	}
 
}</script>
<script language="javascript" src="js/ajax.js"></script>
<script language="javascript" src="js/ajaxresponse.js"></script>
<script language="javascript">
function selfclose()
{
//self.close();
parent.parent.GB_hide();
}
function submitthisform(sessionid)
{
var adtype = document.getElementById('adtype').value;
	if(sessionid=='main_adgroup')
	{		
		sendRequest('ajax/ajax_custom_adgroup.php','custom_network_type='+adtype,'POST');	
	}
	else
	{	
		sendRequest('ajax/ajax_custom_adgroup.php','custom_network_type='+adtype+'&custom_network_id='+sessionid,'POST');	
	
	}	

}

</script>

<table width="80%" border="0" cellpadding="0" cellspacing="0" class="center">
  <tr>
    <td align="right"><img src="images/red_table_ltop.jpg" alt="" /></td>
    <td class="red_table_top"></td>
    <td><img src="images/red_table_rtop.jpg" alt="" /></td>
  </tr>
  <tr>
    <td class="red_table_LSide" style="background-position:right;">&nbsp;</td>
    <!-- ban body part -->
    <td class="inner_contentArea" align="center">
<form name="frmTargets" method="POST" action="" id="frmTargets">
<input type="hidden" name="passValue1" id="passValue1" value="categoryList">
<input type="hidden" name="passValue2" id="passValue2" value="selCategory[]">
<table width="750" cellspacing="0" cellpadding="0" border="0">
        <TR>
          <TD align="center" class="sky font-18"><?php echo CUSTOMNETWORKTARGETING; ?></TD>
        </TR>
        <TR>
          <TD align="left">&nbsp;<?php echo CUSTOMNETWORKMSG; ?></TD>
        </TR>
        <TR>
          <TD align="left">&nbsp;</TD>
        </TR>
        <TR>
          <TD align="left">&nbsp;</TD>
        </TR>
        <TR>
          <TD align="left"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
              <TR>
                <TD>&nbsp;</TD>
              </TR>
              <TR>
                <TD><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                    <TR>
                      <TD width="258" align="left"><strong>Ad Type</strong>&nbsp;
                     	<select id="adtype" name="adtype" class="InputBox" style="width:150px;" onchange="return type_setting();">
                          <option <?if($row_check['ad_type'] == 'text'){ ?> selected="selected"  <? } ?> value="text">Text</option>
                          <option value="thumbnail" <?if($row_check['ad_type'] == 'thumbnail'){ ?> selected="selected"  <? } ?>>Thumbnail</option>
                          <option value="banner" <?if($row_check['ad_type'] == 'banner'){ ?> selected="selected"  <? } ?>>Banner</option>
                        </select>  
						</TD>
                      <TD align="left" width="39">&nbsp;</TD>
                      <TD width="241" align="left"><strong>Bid</strong>&nbsp;<strong>$</strong>
                        <input id="maxBidInput" name="max_bid" style="margin-left: 5px; width: 50px;" type="text" class="InputBox" value="0.5">
                        &nbsp; <strong>Per</strong>&nbsp;
                        <input type="text" readonly="true" id="adUnit" name="ad_unit" class="InputBox" style="width:50px;" value="CPC">
                                               </TD>
                      <!--<TD width="158"><strong>Network</strong>&nbsp;
                        <select id="network" name="network" class="InputBox" style="width:150px;">
                          <option label="CPC" value="cpc">Custom</option>
                          <?php
						/*while($fetNetworkAds = mysql_fetch_array($execNetworkAds))
						{

							?>
                          <option label="CPV" value="<?php echo $fetNetworkAds[networkId]; ?>"><?php echo strtoupper($fetNetworkAds[networkType]); ?> - <?php echo ucfirst($fetNetworkAds[networkName]); ?> ( $<?php echo $fetNetworkAds[networkMinPrice].' '.$fetNetworkAds[networkBidType]; */?>)</option>
						<?php 
						//}
						?>
                        </select></TD>-->
                    </TR>

		    <tr>
			<td id="banner_size" style="display:none" nowrap="nowrap"><strong>Banner Size</strong> &nbsp;

			
				<seleCT name="banner_size">
					<opTION value="no_banner">Select</opTION>
					<opTION value="120x240">120x240</opTION>
					<opTION value="120x400">120x400</opTION>
					<opTION value="120x600">120x600</opTION>
					<opTION value="125x125">125x125</opTION>
					<opTION value="150x600">150x600</opTION>
				</seleCT>
			</td>
			

			<TD align="left"><input type="checkbox" name="ron" id="ron" onclick="return disable_unable();" value="ron">RON</td>
		   </tr>

                  </table></TD>
              </TR>
              <TR>
                <TD align="left">
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <TR>
                      <TD align="center">&nbsp;</td>
                    </tr>
                    <TR>
                      <TD align="left"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td width="8">&nbsp;</td>
                          <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="200" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td height="25" style=" font-family: Arial; font-size: 12px; font-weight: bold; color: #000000;">Targeting Options <a href="#" style="font-family: Arial; font-size: 12px; text-decoration: underline;">What's this? </a></td>
                                  </tr>

                                  <tr>
                                    <td><table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
            	                    <tr>
                                    <td align="left" class="listHeader" style="padding-left: 6px;cursor:pointer;" onclick="showHide('tdCategory','tdGeography')"><span onclick="">Category</span></td>
                                  </tr>
								  <tr>
								  <td style="padding-left: 10px;" id="tdCategory">
								
									<select multiple="multiple" id="categoryList" name="categoryList" style="border:none; height:100px;width: 195px;" size="6">
									<?php 
	while($fetchCategory = mysql_fetch_array($execCategory))
	{				
		for($ij=0;$ij<$cat_len;$ij++)
		{ 
			if($category_array[$ij] == $fetchCategory['categoryName']) { $check_exist = 1;} 
		}
			
		if($check_exist != 1) {
		?>				
		<option value="<?php echo $fetchCategory['categoryName']; ?>"><?php echo $fetchCategory['categoryName']; ?></option>

	<?php
				      }					  
	$check_exist = 0;	
	}
									?>
									</select>							</td>
							</tr>
                                </table></td>
                                  </tr>
                                  <tr>
                                    <td height="8px;"></td>
                                  </tr>
                                  <tr>
                                    <td><table width="200" border="0" cellspacing="0" cellpadding="0" class="outlineBorder">
                                      <tr>
                                        <td align="left" class="listHeader" style="padding-left: 6px;cursor:pointer;" onclick="showHide('tdGeography','tdCategory')">Geography</td>
                                      </tr>
                                      <tr>
                                        <td style="padding-left: 10px;display:none;" id="tdGeography">
										 <select multiple="multiple" id="geographyList" name="geographyList" style="border:none; height:100px;width: 195px;" size="6">
                                            <?php 
while($fetchcountry = mysql_fetch_array($execCountry))
{

	for($i=0;$i<$coun_len;$i++)
	{
	$sqlNetworkLOC = "select countryName from country where countryId = '".$country_array[$i]."' ";
 	$res = mysql_query($sqlNetworkLOC);
	$row = mysql_fetch_array($res);
	if($row['countryName'] == $fetchcountry['countryName']){ $check_country = 1 ; }
	}

	if($check_country != 1)
	{
	?>

	<option value="<?php echo $fetchcountry['countryId']; ?>" ><?php echo $fetchcountry['countryName']; ?></option>
	
	<?php 
	}
	$check_country = 0;
}
					   ?>
                                        </select>										</td>
                                      </tr>
                                    </table></td>
                                  </tr>
                                  <tr>
                                    <td height="8px;"></td>
                                  </tr>
                                  <tr>
                                    <td>&nbsp;</td>
                                  </tr>
                                  <tr>
                                    <td>&nbsp;</td>
                                  </tr>
                              </table></td>
                              <td width="100" align="center"><input type="button" name="Submit" value="Add&gt;&gt;" style="width: 80px;" onclick="moveSelectedOptions(document.getElementById('passValue1').value, document.getElementById('passValue2').value, '5', '','>');" /><br /><input onclick="moveSelectedOptions(  document.getElementById('passValue2').value, document.getElementById('passValue1').value, '', '','<');" type="button" name="Submit" value="&lt;&lt;Remove" style="width: 80px;" /></td>
                              <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                  <td height="25" style=" font-family: Arial; font-size: 12px; font-weight: bold; color: #000000;">Your Targets </td>
                                </tr>
                                <tr>
                                  <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td width="49%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                          <td><table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
                                            <tr>
                                              <td align="left" class="listHeader" style="padding-left: 6px;">Category</td>
                                            </tr>
                                            <tr>
                                              <td style="padding-left: 10px;">


							
			<select multiple="multiple" style="border:none; height:90px;width: 190px;" id="selCategory[]" name="selCategory[]" size="6" > 
	
			<? 
				if(mysql_num_rows($res_check)>0){
				for($i=0;$i<$cat_len;$i++)
				{

			?>
				<option value="<?=$category_array[$i]?>"><?=$category_array[$i]?></option>
			<? 	} 				} ?>

			</select>
												&nbsp;</td>
                                            </tr>
                                          </table></td>
                                        </tr>
                                        <tr>
                                          <td height="8px;"></td>
                                        </tr>
                                        <!--<tr>
                                          <td><table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
                                            <tr>
                                              <td align="left" class="listHeader" style="padding-left: 6px;">Site</td>
                                            </tr>
                                            <tr>
                                              <td style="padding-left: 10px;">&nbsp;</td>
                                            </tr>
                                          </table></td>
                                        </tr>-->
                                      </table></td>
                                      <td width="2%" valign="top">&nbsp;</td>
                                      <td width="49%" valign="top"><table width="200" border="0" cellpadding="0" cellspacing="0" class="outlineBorder">
                                        <tr>
                                          <td align="left" class="listHeader" style="padding-left: 6px;">Geography</td>
                                        </tr>
                                        <tr>
                                          <td style="padding-left: 10px;">
					<select multiple="multiple" style="border:none; height:100px;width: 190px;" id="selGeography[]" name="selGeography[]" size="6">
			<? 
				if(mysql_num_rows($res_check)>0){
				for($i=0;$i<$coun_len;$i++)
				{
	$sqlNetworkLOC = "select countryName from country where countryId = '".$country_array[$i]."' ";
 	$res = mysql_query($sqlNetworkLOC);
	$row = mysql_fetch_array($res);
			?>
				<option value="<?=$country_array[$i]?>"><?=$row['countryName']?></option>
			<? } } ?>


					</select></td>
                                        </tr>
                                      </table></td>
                                      </tr>
                                  </table></td>
                                </tr>
                              </table></td>
                            </tr>
                          </table></td>
                          <td width="8">&nbsp;,<inPUT type="hidden" name="btnSaveNetwork" value="Save"></td>
                        </tr>
                      </table></td>
                    </tr>
                    <TR>
                      <TD align="right"><input type="button" onclick="submitthisform('<?php echo $_SESSION['form_id'];?>')" value="Save" />&nbsp;<input type="submit" value="Cancel" onclick="return selfclose();" />&nbsp;</td>
                    </tr>
                    <TR>
                      <TD align="right">&nbsp;</td>
                    </tr>
                 </table></TD>
              </TR>
            </table></TD>
        </TR>
      </table></form></TD>
<td class="red_table_RSide"></td>
</tr>
<tr>
  <td align="right"><img src="images/red_table_lbottom.jpg" alt="" /></td>
  <td class="red_table_bottom"></td>
  <td><img src="images/red_table_rbottom.jpg" alt="" /></td>
</tr>
</table>

<?php
}
?>